{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单链表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单链表节点类，定义值和指向\n",
    "class ListNode:\n",
    "    def __init__(self, val=None, Next=None):\n",
    "        self.data = val\n",
    "        self.next = Next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LinkedList:\n",
    "    def __init__(self):                                   # 初始化\n",
    "        self.dummy_head = ListNode()                      ## 虚拟头节点\n",
    "        self.size = 0                                     ## 链表长度\n",
    "        \n",
    "    def create(self, val):                                # 创建链表\n",
    "        last_node = self.dummy_head\n",
    "        while last_node.next:\n",
    "            last_node = last_node.next                    ## 搜索到当前链表的最后一个节点\n",
    "        last_node.next = ListNode(val)                    ## 将新节点加入\n",
    "        self.size +=1                                     ## 链表长度加一\n",
    "        \n",
    "    def get_val(self, index):                           # 值搜索——根据链表索引\n",
    "        if index < 0 or index >= self.size:\n",
    "            return -1                                     ## 不合规索引输入，返回-1\n",
    "        \n",
    "        cur_node = self.dummy_head.next        \n",
    "        for i in range(index):\n",
    "            cur_node = cur_node.next                      ## 指针移动到索引点\n",
    "        return cur_node.data\n",
    "    \n",
    "    def add_by_index(self, index, val):                  # 索引添加\n",
    "        if index < 0 or index > self.size:\n",
    "            return                                       ## 不合规不操作\n",
    "        \n",
    "        cur_node = self.dummy_head\n",
    "        for i in range(index):\n",
    "            cur_node = cur_node.next                     ## 到添加位的前一个节点\n",
    "        cur_node.next = ListNode(val, cur_node.next)     ## 注意先后顺序：先创建新节点，并且将前一节点的next作为当前新节点的next；再将新节点作为前一节点的next\n",
    "        self.size +=1\n",
    "        \n",
    "    def delete_by_index(self, index):                    # 索引删除\n",
    "        if index < 0 or index > self.size:\n",
    "            return                                       ## 不合规不操作\n",
    "        \n",
    "        cur_node = self.dummy_head\n",
    "        for i in range(index):\n",
    "            cur_node = cur_node.next                     ## 到删除位的前一个节点\n",
    "        cur_node.next = cur_node.next.next               \n",
    "        self.size -=1\n",
    "        \n",
    "    def add_head(self, val):                             # 添加头节点\n",
    "        self.dummy_head.next = ListNode(val, self.dummy_head.next)\n",
    "        self.size +=1\n",
    "        \n",
    "    def add_tail(self, val):                             # 添加尾节点\n",
    "        cur_node = self.dummy_head\n",
    "        while cur_node.next:\n",
    "            cur_node = cur_node.next\n",
    "        cur_node.next = ListNode(val)\n",
    "        self.size +=1\n",
    "        \n",
    "    def print_list(self):\n",
    "        cur_node = self.dummy_head\n",
    "        while cur_node:\n",
    "            print(cur_node.data, end=\" -> \")\n",
    "            cur_node = cur_node.next\n",
    "        print(\"None\")                                   ## 遍历链表打印\n",
    "        print(self.size)                                ## 打印当前链表长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 1 -> 2 -> 3 -> None\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "linked_list = LinkedList()\n",
    "\n",
    "# 从数组添加元素\n",
    "for i in [1,2,3]:\n",
    "    linked_list.create(i)\n",
    "# 打印链表及长度\n",
    "linked_list.print_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 3, -1)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引搜索值\n",
    "linked_list.get_index(0),linked_list.get_index(2),linked_list.get_index(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> None\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 索引添加节点\n",
    "## 头添加\n",
    "linked_list.add_by_index(0,0)\n",
    "linked_list.print_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> 4 -> None\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "## 尾添加\n",
    "linked_list.add_by_index(4,4)\n",
    "linked_list.print_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> a -> 3 -> 4 -> None\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "## 中间添加\n",
    "linked_list.add_by_index(3,'a')\n",
    "linked_list.print_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 1 -> 2 -> 3 -> None\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 索引删除节点\n",
    "linked_list.delete_by_index(0)\n",
    "linked_list.delete_by_index(4)\n",
    "linked_list.delete_by_index(2)\n",
    "linked_list.print_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> 4 -> None\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# 头尾节点添加\n",
    "linked_list.add_head(0)\n",
    "linked_list.add_tail(4)\n",
    "linked_list.print_list()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
